package com.keldor.controller;

import java.security.Principal;
import java.util.List;

import javax.servlet.http.HttpSession;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.keldor.dao.ArticuloDAOMySQLImpl;
import com.keldor.model.Familia;
import com.keldor.service.ArticuloService;
import com.keldor.service.UsuarioService;
 
@Controller
public class LoginController {
	@Autowired
	private ArticuloService articuloService;
	@Autowired
	private UsuarioService usuarioService;
	
 
	@RequestMapping(value="/welcome", method = RequestMethod.GET)
	public String printWelcome(ModelMap model, Principal principal ) {
//		Recuperamos las credenciales del usuario de la session
		User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//		Comprobamos las credenciales del usuario y redirigimos
		if(AuthorityUtils.authorityListToSet(user.getAuthorities()).contains("ROLE_USER")){
			String name = principal.getName();
			List<Familia> familias = articuloService.getFamilias();
			model.addAttribute("username", name);
			model.addAttribute("message", "PAGINA DE USUARIO");
			model.addAttribute("familias",familias);
			return "hello";
		} else if(AuthorityUtils.authorityListToSet(user.getAuthorities()).contains("ROLE_ADMIN")){
			String name = principal.getName();
			model.addAttribute("username", name);
			model.addAttribute("message", "PAGINA DE ADMINISTRADOR");
			model.addAttribute("familias", articuloService.getFamilias());
			model.addAttribute("valorTotal", articuloService.getValorTotal());
			model.addAttribute("stockTotal", articuloService.getStockTotal());
			model.addAttribute("familiasFinal",articuloService.getFamiliasFinal());
			return "stock";
		}
		
		String name = principal.getName();
		model.addAttribute("username", name);
		model.addAttribute("message", "Spring Security Custom Form example");
		return "hello";
 
	}
	
	@RequestMapping(value="/stock", method = RequestMethod.GET)
	public String pstock(ModelMap model) {
//		Recuperamos las credenciales del usuario de la session
		User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//		Comprobamos las credenciales del usuario y redirigimos
		if(AuthorityUtils.authorityListToSet(user.getAuthorities()).contains("ROLE_ADMIN")){
			String name = user.getUsername();
			model.addAttribute("username", name);
			model.addAttribute("message", "PAGINA DE ADMINISTRADOR");
			model.addAttribute("familias", articuloService.getFamilias());
			model.addAttribute("valorTotal", articuloService.getValorTotal());
			model.addAttribute("stockTotal", articuloService.getStockTotal());
			model.addAttribute("familiasFinal",articuloService.getFamiliasFinal());
			return "stock";
		}
		
		return "stock";
	}
	
	@RequestMapping(value="/historial", method = RequestMethod.GET)
	public String historial(ModelMap model) {	
		return "historial";
	}
	
	@RequestMapping(value="/usuario", method = RequestMethod.GET)
	public String usuario(ModelMap model) {
		model.addAttribute("usuarios",usuarioService.getUsuarios());
		return "usuario";
	}
 
	@RequestMapping(value="/login", method = RequestMethod.GET)
	public String login(ModelMap model) {
 
		return "login";
 
	}
 
	@RequestMapping(value="/loginfailed", method = RequestMethod.GET)
	public String loginerror(ModelMap model) {
 
		model.addAttribute("error", "true");
		return "login";
 
	}
 
	@RequestMapping(value="/logout", method = RequestMethod.GET)
	public String logout(ModelMap model) {
 
		return "login";
 
	}
 
}